package cn.echoedu.specifics;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;

import cn.echoedu.daoconstants.TableName;
import cn.echoedu.entity.AClass;
import cn.echoedu.entity.ClassNotify;
import cn.echoedu.entity.Student;
import cn.echoedu.util.DateUtil;
import cn.echoedu.util.L;
import cn.echoedu.util.StringUtil;

public class SpecClassNotify extends BaseSpec{
	private final String T = "SpecClassNotify-------->";
	@Override
	public <T> PreparedStatement getPreparedInsert(Connection con,
			TableName tableName, T currentPojo) throws SQLException {
		// TODO Auto-generated method stub
		ClassNotify classnotify = (ClassNotify)currentPojo;
		String sql = "insert into classnotify values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
		PreparedStatement preparedstatement = con.prepareStatement(sql);
		preparedstatement.setInt(1, classnotify.getClassnotify());
		if(classnotify.getTitle()==null){
			preparedstatement.setNull(2,Types.VARCHAR);			
		}else{
			preparedstatement.setString(2, classnotify.getTitle());
		}
		if(classnotify.getPlace()==null){
			preparedstatement.setNull(3,Types.VARCHAR);	
		}else{
			preparedstatement.setString(3, classnotify.getPlace());
		}
		if(classnotify.getContent()==null){
			preparedstatement.setNull(4,Types.VARCHAR);	
		}else{
			preparedstatement.setString(4, classnotify.getContent());
		}
		if(classnotify.getFilepath()==null){
			preparedstatement.setNull(5,Types.VARCHAR);	
		}else{
			preparedstatement.setString(5, classnotify.getFilepath());
		}
		if(classnotify.getStarttime()==null){
			preparedstatement.setNull(6,Types.DATE);
		}else{
			preparedstatement.setTimestamp(6,classnotify.getStarttime());
		}
		if(classnotify.getEndtime()==null){
			preparedstatement.setNull(7,Types.DATE);
		}else{
			preparedstatement.setTimestamp(7, classnotify.getEndtime());
		}
		if(classnotify.getRemindertime()==null){
			preparedstatement.setNull(8,Types.DATE);
		}else{
			preparedstatement.setTimestamp(8,classnotify.getRemindertime());
		}

		preparedstatement.setInt(9,classnotify.getShowinhomepage());
		preparedstatement.setInt(10,classnotify.getHitcount());
		if(classnotify.getPublishtime()==null){
			preparedstatement.setNull(11,Types.DATE);
		}else{
			preparedstatement.setTimestamp(11, classnotify.getPublishtime());
		}
		if(classnotify.getStudent()==null||classnotify.getStudent().getStudentnumber()==null){
			preparedstatement.setNull(12,Types.INTEGER);
		}else{
			preparedstatement.setString(12,classnotify.getStudent().getStudentnumber());
		}
		if(classnotify.getAclass()==null||classnotify.getAclass().getClasscode()==0){
		     preparedstatement.setNull(13,Types.INTEGER);
		}else{
			preparedstatement.setInt(13,classnotify.getAclass().getClasscode());
		}
		preparedstatement.setInt(14,classnotify.getLikenum());
		preparedstatement.setInt(15,classnotify.getDislikenum());
		return preparedstatement;
	}

	@Override
	public <T> PreparedStatement getPreparedInsertRunning(Connection con,
			TableName tableName, T currentPojo) throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object getPojo(ResultSet rs) throws SQLException {
		// TODO Auto-generated method stub
		ClassNotify classnotify = new ClassNotify();
		classnotify.setClassnotify(rs.getInt(1));
		classnotify.setTitle(rs.getString(2));
		classnotify.setPlace(rs.getString(3));
		classnotify.setContent(rs.getString(4));
		classnotify.setFilepath(rs.getString(5));
		classnotify.setStarttime(rs.getTimestamp(6));
		classnotify.setEndtime(rs.getTimestamp(7));
		classnotify.setRemindertime(rs.getTimestamp(8));
		classnotify.setShowinhomepage(rs.getInt(9));
		classnotify.setHitcount(rs.getInt(10));
		classnotify.setPublishtime(rs.getTimestamp(11));
		Student student = new Student();
		student.setStudentnumber(rs.getString(12));
		classnotify.setStudent(student);
		AClass aclass = new AClass();
		aclass.setClasscode(rs.getInt(13));
		classnotify.setAclass(aclass);
		classnotify.setLikenum(rs.getInt(14));
		classnotify.setDislikenum(rs.getInt(15));
		return classnotify;
	}

	@Override
	public <T> PreparedStatement getPreparedUpdate(Connection con,
			TableName tableName, T oldPojo, T currentPojo) throws SQLException {
		// TODO Auto-generated method stub
		ClassNotify oldclassnotify = (ClassNotify)oldPojo;
		ClassNotify classnotify = (ClassNotify)currentPojo;
		String sql = "update classnotify set title=?,place=?,content=?,filepath=?," +
				"starttime=?,endtime=?,remindertime=?,showinhomepage=?,hitcount=?," +
				"publishtime=?,studentnumber=?,classcode=?,likenum=?,dislikenum=?" +
				" where classnotifyid=?";
		PreparedStatement preparedstatement = con.prepareStatement(sql);
		if(classnotify.getTitle()==null){
			preparedstatement.setNull(1,Types.VARCHAR);			
		}else{
			preparedstatement.setString(1, classnotify.getTitle());
		}
		if(classnotify.getPlace()==null){
			preparedstatement.setNull(2,Types.VARCHAR);	
		}else{
			preparedstatement.setString(2, classnotify.getPlace());
		}
		if(classnotify.getContent()==null){
			preparedstatement.setNull(3,Types.VARCHAR);	
		}else{
			preparedstatement.setString(3, classnotify.getContent());
		}
		if(classnotify.getFilepath()==null){
			preparedstatement.setNull(4,Types.VARCHAR);	
		}else{
			preparedstatement.setString(4, classnotify.getFilepath());
		}
		if(classnotify.getStarttime()==null){
			preparedstatement.setNull(5,Types.DATE);
		}else{
			preparedstatement.setTimestamp(5,classnotify.getStarttime());
		}
		if(classnotify.getEndtime()==null){
			preparedstatement.setNull(6,Types.DATE);
		}else{
			preparedstatement.setTimestamp(6,classnotify.getEndtime());
		}
		if(classnotify.getRemindertime()==null){
			preparedstatement.setNull(7,Types.DATE);
		}else{
			preparedstatement.setTimestamp(7,  classnotify.getRemindertime());
		}

		preparedstatement.setInt(8,classnotify.getShowinhomepage());
		preparedstatement.setInt(9,classnotify.getHitcount());
		if(classnotify.getPublishtime()==null){
			preparedstatement.setNull(10,Types.DATE);
		}else{
			preparedstatement.setTimestamp(10,classnotify.getPublishtime());
		}
		if(classnotify.getStudent()==null||classnotify.getStudent().getStudentnumber()==null){
			preparedstatement.setNull(11,Types.INTEGER);
		}else{
			preparedstatement.setString(11,classnotify.getStudent().getStudentnumber());
		}
		if(classnotify.getAclass()==null||classnotify.getAclass().getClasscode()==0){
		     preparedstatement.setNull(12,Types.INTEGER);
		}else{
			preparedstatement.setInt(12,classnotify.getAclass().getClasscode());
		}
		preparedstatement.setInt(13,classnotify.getLikenum());
		preparedstatement.setInt(14,classnotify.getDislikenum());
		
		preparedstatement.setInt(15,classnotify.getClassnotify());
		return preparedstatement;
	}

	@Override
	public <T> String getPreparedInsertGetPrimaryKeySql(Connection con,
			TableName tableName, T currentPojo) throws SQLException {
		final String M ="getPreparedInsertGetPrimaryKeySql*******>>";
		L.p(T+M);
		ClassNotify classnotify = (ClassNotify)currentPojo;
		String sql =  " insert into classnotify (" +
				"title," +
				"place,content,filepath," +
				"starttime, endtime, remindertime,showinhomepage,hitcount," +
				"publishtime,studentnumber,classcode,likenum,dislikenum) "+
				"  values"+StringUtil.getBeforeTheBsrackets()
				+StringUtil.varToSqlObject(classnotify.getTitle())
				+StringUtil.getComma()+StringUtil.varToSqlObject(classnotify.getPlace())
				+StringUtil.getComma()+StringUtil.varToSqlObject(classnotify.getContent())
				+StringUtil.getComma()+StringUtil.varToSqlObject(classnotify.getFilepath())
				+StringUtil.getComma()+StringUtil.varToSqlObject(classnotify.getStarttime())
				+StringUtil.getComma()+StringUtil.varToSqlObject(classnotify.getEndtime())
				+StringUtil.getComma()+StringUtil.varToSqlObject(classnotify.getRemindertime())
				+StringUtil.getComma()+StringUtil.varToSqlObject(classnotify.getShowinhomepage())
				+StringUtil.getComma()+StringUtil.varToSqlObject(classnotify.getHitcount())
				+StringUtil.getComma()+StringUtil.varToSqlObject(classnotify.getPublishtime())
				+StringUtil.getComma()+StringUtil.varToSqlObject(classnotify.getStudent().getStudentnumber())
				+StringUtil.getComma()+StringUtil.varToSqlObject(classnotify.getAclass().getClasscode())
				+StringUtil.getComma()+StringUtil.varToSqlObject(classnotify.getLikenum())
				+StringUtil.getComma()+StringUtil.varToSqlObject(classnotify.getDislikenum())
				+StringUtil.getAfterTheBrackets();
		L.p(T+M+sql);
		StringBuilder sqlstr = new StringBuilder();
		sqlstr.append("insert into classnotify (");
		if(classnotify.getTitle()!=null){
			sqlstr.append("title,");
		}
		if(classnotify.getPlace()!=null){
			sqlstr.append("place,");
		}
		if(classnotify.getContent()!=null){
			sqlstr.append("content,");
		}
		if(classnotify.getFilepath()!=null){
			sqlstr.append("filepath,");
		}
		if(classnotify.getStarttime()!=null){
			sqlstr.append("starttime,");
		}
		if(classnotify.getEndtime()!=null){
			sqlstr.append("endtime,");
		}
		if(classnotify.getRemindertime()!=null){
			sqlstr.append("remindertime,");
		}
		if(classnotify.getPublishtime()!=null){
			sqlstr.append("publishtime,");
		}
		if(classnotify.getStudent().getStudentnumber()!=null){
			sqlstr.append("studentnumber,");
		}
		return sql;
	}

	@Override
	public Object getLeftJoinPojo(ResultSet rs) throws SQLException {
		// TODO Auto-generated method stub

		return null;
	}

	@Override
	public Object getPojoBySql(ResultSet rs) throws SQLException {
		// TODO Auto-generated method stub
		ClassNotify classnotify = new ClassNotify();
		classnotify.setClassnotify(rs.getInt("classnotify.classnotifyid"));
		classnotify.setTitle(rs.getString("classnotify.title"));
		classnotify.setPlace(rs.getString("classnotify.place"));
		classnotify.setContent(rs.getString("classnotify.content"));
		classnotify.setFilepath(rs.getString("classnotify.filepath"));
		classnotify.setStarttime(rs.getTimestamp("classnotify.starttime"));
		classnotify.setEndtime(rs.getTimestamp("classnotify.endtime"));
		classnotify.setRemindertime(rs.getTimestamp("classnotify.remindertime"));
		classnotify.setShowinhomepage(rs.getInt("classnotify.showinhomepage"));
		classnotify.setHitcount(rs.getInt("classnotify.hitcount"));
		classnotify.setPublishtime(rs.getTimestamp("classnotify.publishtime"));
		Student student = new Student();
		student.setStudentnumber(rs.getString("classnotify.studentnumber"));
		classnotify.setStudent(student);
		AClass aclass = new AClass();
		aclass.setClasscode(rs.getInt("classnotify.classcode"));
		classnotify.setAclass(aclass);
		classnotify.setLikenum(rs.getInt("classnotify.likenum"));
		classnotify.setDislikenum(rs.getInt("classnotify.dislikenum"));
		return classnotify;
	}

	@Override
	public <T> PreparedStatement getPreparedListInsert(Connection con,
			TableName tableName, T currentPojo) throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public <T> PreparedStatement getPreparedListInsertRunning(Connection con,
			TableName tableName, T currentPojo) throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}

}
